package com.mxpio.erp.equipment.provider;

import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.security.anthentication.ThirdAuthorizeException;
import com.mxpioframework.security.anthentication.ThirdAuthorizeToken;
import com.mxpioframework.security.anthentication.ThirdAuthorizeUserProvider;
import com.mxpioframework.security.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author : wpp
 * @Datetime : 2023/10/30 13:57
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc : //简单三方登录，由前端拿到用户三方ID，匹配用户信息直接返回
 */
@Component
public class SimpleAuthorizeUserProvider implements ThirdAuthorizeUserProvider {

    @Override
    @Transactional
    public User getUser(String authCode) {
        //直接返回用户信息
        if(StringUtils.isBlank(authCode)){
            throw new ThirdAuthorizeException("authCode传参为空，请检查接口数据！");
        }
        User dbUser = JpaUtil.getOne(User.class, authCode);
        if (dbUser!=null){
            return dbUser;
        }
        List<User> user = JpaUtil.linq(User.class).equal("thirdId", authCode).list();
        if (user==null||user.size()==0){
            String msg = "三方用户ID未能在应用系统中匹配到用户，请检查用户管理第三方账号信息是否已绑定或者有效";
            throw new ThirdAuthorizeException(msg);
        }
        return user.get(0);
    }

    @Override
    public boolean support(ThirdAuthorizeToken thirdAuthorizeToken) {
        return StringUtils.equals(thirdAuthorizeToken.getThirdPlatformType().toString(),"SIMPLE");//SIMPLE
    }
}
